package com.djl.mymall.dto;

import com.djl.mymall.mbg.entity.Admin;
import com.djl.mymall.mbg.entity.Permission;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: myMall
 * @description:
 * @author: Mr.dai
 * @create: 2022-01-20 11:01
 **/
@AllArgsConstructor
@NoArgsConstructor
public class AdminUserDetails implements UserDetails {
    private Admin admin;
    private List<Permission> permissionList;

    /**
     * 返回当前全限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return  permissionList.stream()
                .filter(permission -> permission.getValue()!=null)
                .map(permission -> new SimpleGrantedAuthority(permission.getValue()))
                .collect(Collectors.toList());
    }


    @Override
    public String getPassword() {
        return admin.getPassword();
    }

    @Override
    public String getUsername() {
        return admin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return admin.getStatus().equals(1);
    }
}
